const { pro_product, pro_type, usr_message, op, querySql } = require("../../middleware/baseModel");

module.exports = {
  "GET /product/types": async (ctx, next) => {
    const res = await pro_type.findAll({ order: [["sort", "ASC"]] });
    return ctx.success(res);
  },

  "POST /product/page": async (ctx, next) => {
    let param = ctx.getPageSize();
    let row = ctx.getBody();
    let { type_id } = row;
    let { key, value } = row.seachOption;
    let where = {
      is_publish: 1,
      type_id,
    };
    if (key && value) {
      where[key] = { [op.like]: "%" + value + "%" };
    }

    const res = await pro_product.findAndCountAll({
      where,
      order: [["id", "DESC"]],
      ...param,
    });
    return ctx.success(res);
  },
  "GET /product/detail": async (ctx, next) => {
    let id = ctx.get("id");

    let nextRow = await querySql(`select id,title from  pro_product where id<${id} order by id desc limit 0,1`);
    const res = await pro_product.findOne({ where: { id: id } });
    let upRow = await querySql(`select id,title from  pro_product where id>${id} order by id asc limit 0,1`);
    return ctx.success({
      curRow: res,
      upRow,
      nextRow,
    });
  },
  "POST /product/msg": async (ctx, next) => {
    let row = ctx.getBody();
    const res = await usr_message.create(row);
    return ctx.success(res);
  },
};
